package com.ky.facescan.utils;


import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ky.facescan.R;


public class LoadingDialog {
    static Dialog dialog;

    public static void show(Context context, String msg) {
        dismiss();
        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.dialog_loading, null);// 得到加载view
        LinearLayout layout = v
                .findViewById(R.id.dialog_loading_view);// 加载布局
        TextView tipTextView = v.findViewById(R.id.tipTextView);// 提示文字
        tipTextView.setText(msg);// 设置加载信息

        dialog = new Dialog(context, R.style.LoadingDialogStyle);// 创建自定义样式dialog
        dialog.setCancelable(true); // 是否可以按“返回键”消失
        dialog.setCanceledOnTouchOutside(false); // 点击加载框以外的区域
        dialog.setContentView(layout, new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));// 设置布局
        /**
         *将显示Dialog的方法封装在这里面
         */
        Window window = dialog.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setGravity(Gravity.CENTER);
        window.setAttributes(lp);
        window.setWindowAnimations(R.style.PopWindowAnimStyle);
        window.setDimAmount(0.3f);//灰度
        try {
            if (context instanceof Activity) {
                Activity activity = (Activity) context;
                boolean finishing = activity.isFinishing();
                LogUtils.e("isFinishing-show" + finishing);
            }
            dialog.show();
        } catch (Exception e) {
//            e.printStackTrace();
        }
    }

    /**
     * 关闭dialog
     */
    public static void dismiss() {
        try {
            if (dialog != null) {
                dialog.dismiss();
                dialog = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
